package com.example.hcd.functionactivity;

import java.util.List;

import com.example.hcd.datamanage.UserInfo;
import com.example.hcd.internet.InternetKeep;
import com.example.hcd.tool.MyActivityManager;
import com.umeng.analytics.MobclickAgent;

import android.app.Activity;
import android.app.ActivityManager;
import android.app.ActivityManager.RunningAppProcessInfo;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

import cn.jpush.android.api.JPushInterface;

public class BaseActivity extends Activity {

    public  String mPageName = "BaseActivity";

    protected MyActivityManager activityManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityManager = MyActivityManager.getScreenManager();
        activityManager.pushActivity(this);
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        super.onStop();

        if (!isAppOnForeground()) {
            //app 进入后台
            //全局变量
            UserInfo.getUserInfor().isActive = false; //记录当前已经进入后台
        }
    }

    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        activityManager = MyActivityManager.getScreenManager();
        activityManager.pushActivity(this);

    }

    @Override
    protected void onDestroy() {
        activityManager.popActivity(this);
        super.onDestroy();
    }


    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        super.onResume();
        MobclickAgent.onPageStart(mPageName);
        MobclickAgent.onResume(this);
        JPushInterface.onResume(this);
        if (!UserInfo.getUserInfor().isActive) {
            //app 从后台唤醒，进入前台

            UserInfo.getUserInfor().isActive = true;

            if (!UserInfo.getUserInfor().isUsePhoto) {
//            		UserInfo.getUserInfor().setmBitmap(null);
//    				//unregisterReceiver(mBroadcastReceiver);
//                	Intent intent = new Intent(this,LoginActivity.class);
//    				startActivity(intent);
//    				finish();
            }


        }
    }

    @Override
    protected void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(mPageName);
        MobclickAgent.onPause(this);
        JPushInterface.onPause(this);
    }

    /**
     * 程序是否在前台运行
     *
     * @return
     */
    public boolean isAppOnForeground() {
        // Returns a list of application processes that are running on the
        // device

        ActivityManager activityManager = (ActivityManager) getApplicationContext().getSystemService(Context.ACTIVITY_SERVICE);
        String packageName = getApplicationContext().getPackageName();

        List<RunningAppProcessInfo> appProcesses = activityManager
                .getRunningAppProcesses();
        if (appProcesses == null)
            return false;

        for (RunningAppProcessInfo appProcess : appProcesses) {
            // The name of the process that this object is associated with.
            if (appProcess.processName.equals(packageName)
                    && appProcess.importance == RunningAppProcessInfo.IMPORTANCE_FOREGROUND) {
                return true;
            }
        }

        return false;
    }

    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    }


    @Override
    public void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        if(!InternetKeep.getInstence().isSystemInfoLoad)
        {
            Log.e("restore","onRestoreInstanceState");
            InternetKeep.getInstence().getSystemInfo();
        }

    }
}
